<!DOCTYPE html>
<html lang="zh-CN">

<head>

    <meta http-equiv="Expires" content="0">
    <meta http-equiv="Pragma" content="no-cache">
    <meta http-equiv="Cache" content="no-cache">
    <meta http-equiv="Cache-Control" content="no-cache" />

    <meta charset="utf-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0,user-scalable=no">
    <meta name="description" content="长沙市妇幼医院">
    <title>长沙市健康民生项目</title>
    <script src="./js/jquery.min.js"></script>
    <script src="js/jweixin-1.2.0.js" type="text/javascript" charset="utf-8"></script>
    <style>
        #sampleCode{
            display: none;
            padding: 15px;
            background: #337ab7;
            margin-top: 30px;
            font-size: 16px;
            color: #fff;
            border-radius: 5px;
        }
        #saoMa{
            padding: 80px 0;
            font-size: 18px;
            display: none;
        }
        #saoMa div{
            margin-top: 50px;
            text-align: center;
        }
        #saoMa div a{
            display: inline-block;
            text-decoration:none;
            font-size: 16px;
            color: #fff;
            background: #337ab7;
            width: 80px;
            height: 50px;
            line-height: 50px;
            border-radius: 5px;
            text-align: center;
        }
        #saoMa div .a-left{
            margin-right: 30px;
        }
    </style>
</head>
	<script type="text/javascript">
        var couponNum = "";
        var openId = "";
        var productCode = "";
		$(document).ready(function(){
			$.ajax({
				type:"post",
				url:"/csfy/wx/getWxConfig.do",
				data:{
				    url:window.location.href
                },
			    success : function(data) {
				    var getData = JSON.parse(data);
				    var appId = getData.appId;
				    var timestamp = getData.timestamp;
				    var nonceStr = getData.nonceStr;
				    var signature = getData.signature;
			    	wx.config({
					    debug: false, // 开启调试模式,调用的所有api的返回值会在客户端alert出来，若要查看传入的参数，可以在pc端打开，参数信息会通过log打出，仅在pc端时才会打印。
					    appId: appId, // 必填，公众号的唯一标识
					    timestamp: timestamp, // 必填，生成签名的时间戳
					    nonceStr: nonceStr, // 必填，生成签名的随机串
					    signature: signature,// 必填，签名
					    jsApiList: ['scanQRCode'] // 必填，需要使用的JS接口列表
					});
					//调用微信扫码
                    wx.ready(function () {
                        wx.scanQRCode({
                            needResult: 1, // 默认为0，扫描结果由微信处理，1则直接返回扫描结果，
                            scanType: ["qrCode","barCode"], // 可以指定扫二维码还是一维码，默认二者都有
                            success: function (res) {
                                var result = res.resultStr.substr(res.resultStr.indexOf('=')+1); // 当needResult 为 1 时，扫码返回的结果
                                couponNum = result;
                                var url = window.location.href;
                                openId = url.substr(url.indexOf('=')+1);
//                                alert("openId:"+openId+"二维码信息:"+result);
                                $.ajax({
                                    type:"get",
                                    url:"/csfy/wx/getCouponInformation.do",
                                    data:{
                                        couponNum:result
                                    },
                                    async:true,
                                    success : function(data) {
                                        var dataInfo = JSON.parse(data);
                                        if(dataInfo.code == 200){
                                            var isCharge = "免费";
                                            if (dataInfo.rows.isCharge == "2") {
                                                isCharge = "自费"
                                            }
                                            var showInfo = "当前扫描类型为： " + '<span style="color: red;font-size: 18px">' + dataInfo.rows.couponBaseName + '</span>'+ '<span style="color: red;font-size: 18px"> - ' + isCharge + '</span>' + " ,请确认是否扫码";
                                            $("#couponName").html(showInfo);
                                            $("#saoMa").show();
                                        }else {
                                            alert(dataInfo.msg);
                                        }
                                    }
                                });
                            }
                        });
                    });
			    }
			});
		})
        //扫采血卡的条形码
        function sampleCodeClick() {
            //调用微信扫码
            wx.ready(function () {
                wx.scanQRCode({
                    needResult: 1, // 默认为0，扫描结果由微信处理，1则直接返回扫描结果，
                    scanType: ["qrCode","barCode"], // 可以指定扫二维码还是一维码，默认二者都有
                    success: function (res) {
                        var result = res.resultStr.substr(res.resultStr.indexOf(",") + 1);// 当needResult 为 1 时，扫码返回的结果
//                        alert("条形码：" + result)
                        if((result.length != 11 && productCode == "BS0002") || (result.length != 10 && productCode == "DX0459")){
                            alert("当前扫描条形码为：" + result + ",长度有误，请重新扫描采血卡");
                            return false;
                        }
                        $.ajax({
                            type:"get",
                            url:"/csfy/wx/scanCouponCodeBoundBarCode.do",
                            data:{
                                couponNum:couponNum,
                                bloodCode:result
                            },
                            async:true,
                            success : function(data) {
                                var dataInfo = JSON.parse(data);
                                if(dataInfo.code == 200){
                                    $("#disDiv").text("送检单与采血卡绑定成功，券码为：" + couponNum + ",采血卡编码为：" + result);
                                    $("#disDiv").css('display','block');
                                    $("#sampleCode").css('display','none');
                                }else {
                                    alert(dataInfo.msg);
                                }
                            }
                        })
                    }
                });
            });
        }
        //确认二维码扫码
        function commit() {
            $.ajax({
                type:"get",
                url:"/csfy/wx/updateSampleBaseByCouponNum.do",
                data:{
                    couponNum:couponNum,
                    openid:openId
                },
                async:true,
                success : function(data) {
                    var dataInfo = JSON.parse(data);
                    if(dataInfo.msg == "success"){
                        $("#saoMa").hide();
                        if(dataInfo.rows.productCode == "BS0002" || dataInfo.rows.productCode =="DX0459"){
                            productCode = dataInfo.rows.productCode;
                            $("#disDiv").html('优惠券扫码成功,请继续扫描 ' + '<span style="color: red;font-size: 25px">采血卡条形码</span>');
                            $("#disDiv").css('display','block');
                            $("#sampleCode").css('display','inline-block');
                        }else {
                            $("#disDiv").text('扫码成功，免费券编码为：' + dataInfo.rows.couponNum);
                            $("#disDiv").css('display','block');
                        }
                    }else {
                        $("#saoMa").hide();
                        if (dataInfo.rows != undefined && dataInfo.rows != null){
                        if(dataInfo.rows.productCode == "BS0002" || dataInfo.rows.productCode =="DX0459"){
                            if(dataInfo.rows.bloodCode == null){
                                productCode = dataInfo.rows.productCode;
                                $("#disDiv").html('优惠券扫码成功,请继续扫描 ' + '<span style="color: red;font-size: 25px">采血卡条形码</span>');
                                $("#disDiv").css('display','block');
                                $("#sampleCode").css('display','inline-block');
                            }else {
                                $("#disDiv").text("优惠券："+couponNum+" 已扫码,且已绑定采血卡:"+dataInfo.rows.bloodCode);
                                $("#disDiv").css('display','block');
                            }
                        }else {
                            alert(dataInfo.msg);
                        }
                    }else {
                            alert(dataInfo.msg);
                        }
                    }
                }
            })
        }
	</script>
<body style="text-align: center">
    <div id="saoMa">
        <p id="couponName"></p>
        <div>
            <a class="a-left" onclick="commit()">确认</a>
            <a class="a-right" href="javascript:location.reload();">取消</a>
        </div>
    </div>
    <div id="disDiv" style="font-size: 24px;text-align: center;margin-top: 100px;display: block;"></div>
    <span id="sampleCode" onclick="sampleCodeClick()">扫描条形码</span>
</body>

</html>